//
//  ViewController.m
//  半糖界面
//
//  Created by 丁侃 on 16/4/19.
//  Copyright © 2016年 丁侃. All rights reserved.
//

#import "ViewController.h"
#define screenWidth [UIScreen mainScreen].bounds.size.width
#define screenHeight [UIScreen mainScreen].bounds.size.height

//图片轮播高度
static CGFloat const iocnViewH = 200;

//titleBar高度
static CGFloat const titleBarH = 44;

//title间距
static CGFloat const titleSpace = 10;

//设置偏移参考
static CGFloat const titleOffsetInstance = 150;

@interface ViewController ()<UIScrollViewDelegate>

@property (nonatomic, weak) UIScrollView *titleBar;

@property (nonatomic, strong) NSMutableArray<UIButton *> *titleBtn;

//保存上一个按钮
@property (nonatomic, strong) UIButton *preBtn;

@property (nonatomic, weak) UIView *moveView;

@end

@implementation ViewController

-(NSMutableArray *)titleBtn
{
    if (!_titleBtn) {
        _titleBtn = [NSMutableArray array];
    }
    return _titleBtn;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    
    //1.添加图片轮播
    [self setupIconsView];
    
    //2.添加标题条
    [self setupTitleBar];
    
    //3.添加主要显示的控制器
    [self setupBottomView];
    
    //初始化颜色和缩放比例
    self.titleColorR = 1;
    self.titleScale = 0.2;

}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //此方法可能会调用多次,但是自控制器只需要添加一次就好了,所以这里需要判断
    if (self.automaticallyAdjustsScrollViewInsets) {
        
        self.automaticallyAdjustsScrollViewInsets = NO;

        //设置titleBar中的按钮
        [self setupTitleBtn];
        
        _bottomView.contentSize = CGSizeMake(self.childViewControllers.count * screenWidth, 0);
        
        _bottomView.pagingEnabled = YES;
        
        _bottomView.showsHorizontalScrollIndicator = NO;
        _bottomView.showsVerticalScrollIndicator = NO;
        
        [self scrollViewDidScroll:self.bottomView];
    }
}

//设置titleBar中的按钮
-(void)setupTitleBtn
{
    NSInteger count = self.childViewControllers.count;
    
    UIViewController *vc = nil;
    
    CGFloat width = 0;
    CGFloat height = 40;
    
    for (NSInteger i = 0; i < count; i ++) {
        
        vc = self.childViewControllers[i];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [btn setTitle:vc.title forState:UIControlStateNormal];
        
        btn.titleLabel.font = [UIFont systemFontOfSize:16.0];
        
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        [btn sizeToFit];
        
        width = btn.bounds.size.width;
        
        [self.titleBtn addObject:btn];
        
        //不等宽按钮排列
        btn.frame = CGRectMake(CGRectGetMaxX(self.titleBar.subviews.lastObject.frame) + titleSpace, 0, width, height);

        
        NSLog(@"%@",NSStringFromCGRect(btn.frame));
        btn.tag = i;
        
        //添加监听
        [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        
        if (i == 0) {
            
            //添加滑块
            UIView *moveView = [[UIView alloc]init];
            moveView.backgroundColor = [UIColor redColor];
            moveView.bounds = CGRectMake(0, 0, 36, 4);
            moveView.center = CGPointMake(btn.center.x, 42);
            
            [self.titleBar addSubview:moveView];
            _moveView = moveView;
            
            [self click:btn];
        }

        [self.titleBar addSubview:btn];
    }
    self.titleBar.contentSize = CGSizeMake(CGRectGetMaxX(self.titleBar.subviews.lastObject.frame) + titleSpace, 0);
    
    self.titleBar.showsHorizontalScrollIndicator = NO;
}

//按钮的点击
-(void)click:(UIButton *)btn
{
    //3.切换界面
    [self setupChildVcView:btn.tag];
    
    //4.切换底部滑块的位置
    [self setupMoveViewFrame:btn];

     self.preBtn = btn;
}


#pragma  mark -------------------------------
#pragma  mark -UIScrollViewDelegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / screenWidth;
    UIButton *btn = self.titleBtn[index];

    //1.添加控制器的view
    [self click:btn];
}

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    //动滚动动画结束,回复其他按钮的颜色和缩放大小
    for (UIButton *otherBtn  in self.titleBtn) {
        if (otherBtn != self.preBtn) {
            [otherBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            otherBtn.transform = CGAffineTransformMakeScale(1, 1);
        }
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //获取索引
    NSInteger currentIndex = scrollView.contentOffset.x / screenWidth;
    
    //获取将要显示的索引
    NSInteger futureIndex = currentIndex + 1;
    
    //索引越界
    if (futureIndex > self.titleBtn.count - 1) {
        futureIndex = self.titleBtn.count - 1;
    }

    //获取当前按钮
    UIButton *currentBtn = self.titleBtn[currentIndex];
    UIButton *futureBtn = self.titleBtn[futureIndex];

    //获取当前按钮的变化比例
    CGFloat futureScale = scrollView.contentOffset.x / screenWidth - currentIndex;
    //获取将要显示的比例
    CGFloat currentScale = 1 - futureScale;

    //修改颜色
    [currentBtn setTitleColor:[self setColorWithScale:currentScale] forState:UIControlStateNormal];
    [futureBtn setTitleColor:[self setColorWithScale:futureScale] forState:UIControlStateNormal];

    //修改形变
    currentBtn.transform = CGAffineTransformMakeScale(1 + self.titleScale * currentScale,1 + self.titleScale * currentScale);
    futureBtn.transform = CGAffineTransformMakeScale(1 + self.titleScale * futureScale,1 + self.titleScale * futureScale);

    [self setupContentOffset:currentBtn];

}

//设置偏移距离
-(void)setupContentOffset:(UIButton *) btn
{
    CGFloat space = btn.center.x - titleOffsetInstance;
    
    if (space < 0) {
        space = 0;
    }
    //如果超出了最大偏移距离就不偏移
    CGFloat maxSpace = self.titleBar.contentSize.width - screenWidth;
    
    if (space > maxSpace) {
        space = self.titleBar.contentSize.width - screenWidth;
    }
    
    [self.titleBar setContentOffset:CGPointMake(space, 0) animated:YES];
}


//设置颜色
-(UIColor *)setColorWithScale:(CGFloat )scale
{
    CGFloat red = self.titleColorR * scale;
    CGFloat green = self.titleColorG * scale;
    CGFloat blue = self.titleColorB * scale;
    
    return [UIColor colorWithRed:red green:green blue:blue alpha:1];
}

//移动滑块的位置
-(void)setupMoveViewFrame:(UIButton *)btn
{
    [UIView animateWithDuration:0.2 animations:^{
        
        self.moveView.center = CGPointMake(btn.center.x, 42);

    }];
}

//根据索引切换界面切换界面
-(void) setupChildVcView:(NSInteger )index
{
    UIViewController *vc = self.childViewControllers[index];
    
    CGFloat width = self.bottomView.bounds.size.width;
    CGFloat height = self.bottomView.bounds.size.height;
    
    vc.view.frame = CGRectMake(index * width, 0, width, height);
    
    //如果已经添加到scroll上,就不需要再次添加
    if (!vc.view.superview) {
        
        [self.bottomView addSubview:vc.view];

    }
    [self.bottomView setContentOffset:CGPointMake(index * width, 0) animated:YES];
    
}

//3.添加主要显示的控制器
-(void) setupBottomView
{
    UIScrollView *bottomView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_titleBar.frame), screenWidth, screenHeight - CGRectGetMaxY(_titleBar.frame))];
    [self.view addSubview:bottomView];
    _bottomView = bottomView;
    _bottomView.delegate = self;
}

 //2.添加标题条
-(void)setupTitleBar
{
    UIScrollView *titleBar = [[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_iconView.frame), screenWidth, titleBarH)];
    [self.view addSubview:titleBar];
    _titleBar = titleBar;
}


//1.添加图片轮播
-(void)setupIconsView
{

    UIView *iconView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, iocnViewH)];
    [self.view addSubview:iconView];
    _iconView = iconView;
    
}


@end
